#include "XiolControllerState.h"

#include <assert.h>

namespace Xiol
{

ControllerState::ControllerState()
{
	ZeroMemory( &mGamepadStruct, sizeof(XINPUT_GAMEPAD) );
	mGamepadStruct.sThumbLX = 0;	// Some signed members of the struct require non-zero-memory initialisation
	mGamepadStruct.sThumbLY = 0;
	mGamepadStruct.sThumbRX = 0;
	mGamepadStruct.sThumbRY = 0;
}

ControllerState::ControllerState( const XINPUT_GAMEPAD& gamepadStruct )
{
	CopyMemory( &mGamepadStruct, &gamepadStruct, sizeof(XINPUT_GAMEPAD) );
}

ControllerState::ControllerState( const ControllerState& other )
{
	CopyMemory( &mGamepadStruct, &other.mGamepadStruct, sizeof(XINPUT_GAMEPAD) );
}

ControllerState& ControllerState::operator=( const ControllerState& other )
{
	if ( &other==this )
		return (*this);
	CopyMemory( &mGamepadStruct, &other.mGamepadStruct, sizeof(XINPUT_GAMEPAD) );
	return (*this);
}

bool ControllerState::isButtonPressed( WORD buttonID ) const
{ 
	return (mGamepadStruct.wButtons & buttonID)!=0; 
}

BYTE ControllerState::getTriggerPosition( int triggerID ) const
{
	if ( triggerID==0 )
		return mGamepadStruct.bLeftTrigger;
	else if ( triggerID==1 )
		return mGamepadStruct.bRightTrigger;
	assert(false);
	return 0;
}

void ControllerState::getThumbstickPosition( int thumbstickID, SHORT& positionX, SHORT& positionY ) const
{
	if ( thumbstickID==0 )
	{
		positionX = mGamepadStruct.sThumbLX;
		positionY = mGamepadStruct.sThumbLY;
	}
	else if ( thumbstickID==1 )
	{
		positionX = mGamepadStruct.sThumbRX;
		positionY = mGamepadStruct.sThumbRY;
	}
	else
	{
		assert(false);
	}
}

}